{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i>Copyright (c) Microsoft Corporation. All rights reserved.</i>\n",
    "\n",
    "<i>Licensed under the MIT License.</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Upload files for HTML User Interface\n",
    "\n",
    "In the notebook [1_image_similarity_export.ipynb](1_image_similarity_export.ipynb) we exported reference image features, reference image file names, and reference image thumbnails. In this notebook we upload those items, as well as our simplified HTML interface, to your Azure Blob storage account for easy public access. \n",
    "\n",
    "You should create an Azure Storage Account and a \"Container\" in that account to store your uploaded files.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ensure edits to libraries are loaded and plotting is shown in the notebook.\n",
    "%matplotlib inline\n",
    "%reload_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Azure Blob Storage SDK Version: 12.2.0\n"
     ]
    }
   ],
   "source": [
    "# Standard python libraries\n",
    "import sys\n",
    "from pathlib import Path\n",
    "from tqdm.notebook import trange, tqdm\n",
    "import os, uuid\n",
    "import azure.storage.blob\n",
    "from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, ContentSettings\n",
    "\n",
    "# Check Storage SDK version number\n",
    "print(f\"Azure Blob Storage SDK Version: {azure.storage.blob.VERSION}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we setup variables to point to your Azure Blob storage account and your existing Blob container. May be best to setup a fresh Blob container for this upload. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "AZURE_ACCOUNT_NAME = \"YOUR ACCOUNT NAME\"\n",
    "AZURE_ACCOUNT_KEY = \"YOUR ACCOUNT ACCESS KEY\"\n",
    "BLOB_CONTAINER_NAME = \"YOUR CONTAINER NAME\"\n",
    "ENDPOINT_SUFFIX = \"core.windows.net\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we upload the files to your Azure Blob storage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading non-image files:\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "94c0257b17f344ab851df91fb07b3e15",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=353000.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Uploading thumbnail image files:\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "290a70054d8344bb964a93b8ff305941",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=469929.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "azure_storage_connection_str = \"DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix={}\".format(AZURE_ACCOUNT_NAME, AZURE_ACCOUNT_KEY, ENDPOINT_SUFFIX)\n",
    "container_name = BLOB_CONTAINER_NAME\n",
    "local_files = ['../UICode/data/ref_filenames.zip','../UICode/data/ref_features.zip','../UICode/index.html','../UICode/script.js','../UICode/example_imgs.js','../UICode/style.css']\n",
    "blob_files = ['data/ref_filenames.zip','data/ref_features.zip','index.html','script.js','example_imgs.js','style.css']\n",
    "\n",
    "# Create the BlobServiceClient object which will be used to create a container client\n",
    "blob_service_client = BlobServiceClient.from_connection_string(azure_storage_connection_str)\n",
    "\n",
    "# Get total size of non-image files to upload\n",
    "sizecounter = 0\n",
    "for file in local_files:\n",
    "    sizecounter += os.stat(file).st_size\n",
    "    \n",
    "print(\"Uploading non-image files:\")\n",
    "\n",
    "# # Upload the individual files for the front-end and the ZIP files for reference features\n",
    "i = 0\n",
    "with tqdm(total=sizecounter, unit='B', unit_scale=True, unit_divisor=1024) as pbar:\n",
    "    while (i < len(local_files)):\n",
    "        # Create a blob client using the local file name as the name for the blob\n",
    "        blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_files[i])\n",
    "\n",
    "        # Upload the file\n",
    "        with open(local_files[i], \"rb\") as data:\n",
    "            buf = 0\n",
    "            buf = os.stat(local_files[i]).st_size\n",
    "            if (i==2):\n",
    "                blob_client.upload_blob(data, overwrite=True, content_settings=ContentSettings(content_type=\"text/html\"))\n",
    "            elif (i==5):\n",
    "                blob_client.upload_blob(data, overwrite=True, content_settings=ContentSettings(content_type=\"text/css\"))\n",
    "            else:\n",
    "                blob_client.upload_blob(data, overwrite=True)\n",
    "            if buf:\n",
    "                pbar.update(buf)\n",
    "        i+=1\n",
    "\n",
    "# Upload the thumbnail versions of the reference images\n",
    "path_blob = 'small-150'\n",
    "path_local = '../UICode/{}'.format(path_blob)\n",
    "\n",
    "# Get total size of all image files to upload\n",
    "sizecounter = 0\n",
    "for root, dirs, files in os.walk(path_local):\n",
    "    for file in files:\n",
    "        sizecounter += os.stat(os.path.join(path_local, file)).st_size\n",
    "\n",
    "print(\"Uploading thumbnail image files:\")\n",
    "\n",
    "with tqdm(total=sizecounter, unit='B', unit_scale=True, unit_divisor=1024) as pbar:\n",
    "    for root, dirs, files in os.walk(path_local):\n",
    "        for file in files:\n",
    "            blob_client = blob_service_client.get_blob_client(container=container_name, blob=path_blob+'/'+file)\n",
    "            with open(os.path.join(path_local, file), \"rb\") as data:\n",
    "                buf = 0\n",
    "                buf = os.stat(os.path.join(path_local, file)).st_size\n",
    "                blob_client.upload_blob(data, overwrite=True)\n",
    "                if buf:\n",
    "                    pbar.set_postfix(file=file, refresh=False)\n",
    "                    pbar.update(buf)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (cv)",
   "language": "python",
   "name": "cv"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
